﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Xml;

namespace RunCMD
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        public delegate void CloseUI();
        public delegate void UpdateFormTitle(string strTitle);

        public void RunCommand()
        {
            CloseUI closeUI = new CloseUI(CloseWin);
            Process process = new Process();
            process.StartInfo.FileName = fileName;
            process.StartInfo.Arguments = arguments;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;

            process.Start();
            foreach (string command in commandArray)
            {
                process.StandardInput.WriteLine(command);
            }

            string myResult = process.StandardOutput.ReadToEnd();
            myResult += process.StandardError.ReadToEnd();
            //process.WaitForExit()
            process.Close();

            Invoke(closeUI);
        }

        #region private variable

        private string fileName = string.Empty;
        private string arguments = string.Empty;
        private List<string> commandArray = new List<string>();

        #endregion

        #region private methods

        private void InitConfig()
        {
            string strConfigPath = Application.StartupPath + "\\config.xml";
            XmlDocument doc = new XmlDocument();
            doc.Load(strConfigPath);

            fileName = doc.SelectSingleNode("//AppConfig/FileName").InnerText;

            XmlNodeList argumentNodes = doc.SelectNodes(@"//AppConfig/Arguments/Argument");
            foreach (XmlNode argument in argumentNodes)
            {
                arguments += " ";
                if (argument.Attributes.Count == 0 && argument.InnerText != string.Empty)
                {
                    arguments += "/" + argument.InnerText;
                }
                else if (((XmlElement)argument).HasAttribute("Name") && argument.InnerText != string.Empty)
                {
                    arguments += "set " + ((XmlElement)argument).GetAttribute("Name") + " = " + argument.InnerText + "&";
                }
            }
            if (arguments.Substring(arguments.Length - 1) == "&")
            {
                arguments = arguments.Substring(0, arguments.Length - 1);
            }

            XmlNodeList commandNodes = doc.SelectNodes(@"//AppConfig/Commands/Command");
            foreach (XmlNode command in commandNodes)
            {
                if (command.InnerText.Trim() != string.Empty)
                {
                    commandArray.Add(command.InnerText);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void StratCommand()
        {
            Thread trd = new Thread(new ThreadStart(this.RunCommand));
            trd.IsBackground = true;
            trd.Start();
        }

        private void CloseWin()
        {
            Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;

            InitConfig();
            StratCommand();
        }

        private void log(string strPath, string msg)
        {
            try
            {
                FileStream fs = new FileStream(strPath, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("[" + System.DateTime.Now.ToLongTimeString() + "] [Debug]");
                sw.WriteLine("Msg: " + msg);
                sw.WriteLine();
                sw.Close();
                fs.Close();
            }
            catch
            {
            }
        }

        #endregion
        
    }
}
